package io.github.echarts.model.geometry;

/**
 * 表示从当前点到指定点的直线路径片段
 * 对应SVG路径中的L命令
 */
public class LineTo implements PathSegment {
    
    /**
     * 目标点
     */
    private final Point point;
    
    /**
     * 创建LineTo路径片段
     * 
     * @param point 目标点
     */
    public LineTo(Point point) {
        if (point == null) {
            throw new IllegalArgumentException("点不能为null");
        }
        this.point = point;
    }
    
    /**
     * 创建LineTo路径片段
     * 
     * @param x 目标点X坐标
     * @param y 目标点Y坐标
     */
    public LineTo(double x, double y) {
        this(new Point(x, y));
    }
    
    /**
     * 获取目标点
     * 
     * @return 目标点
     */
    public Point getPoint() {
        return point;
    }
    
    @Override
    public PathSegmentType getType() {
        return PathSegmentType.LINE_TO;
    }
    
    @Override
    public String toSvgPathData() {
        return "L" + point.getX() + "," + point.getY();
    }
    
    @Override
    public Rectangle getBounds(Point startPoint) {
        if (startPoint == null) {
            // 如果没有起始点信息，只能返回目标点的零尺寸矩形
            return new Rectangle(point.getX(), point.getY(), 0, 0);
        }
        
        // 计算包含起点和终点的矩形
        double minX = Math.min(startPoint.getX(), point.getX());
        double minY = Math.min(startPoint.getY(), point.getY());
        double maxX = Math.max(startPoint.getX(), point.getX());
        double maxY = Math.max(startPoint.getY(), point.getY());
        
        return new Rectangle(minX, minY, maxX - minX, maxY - minY);
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        
        LineTo lineTo = (LineTo) o;
        return point.equals(lineTo.point);
    }
    
    @Override
    public int hashCode() {
        return point.hashCode();
    }
    
    @Override
    public String toString() {
        return "LineTo{" +
                "point=" + point +
                '}';
    }
} 